home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / System 7.0 Samples / MacShell / AppleTalk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-04  |  7.9 KB  |  291 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** Program:         MacShell
  5. ** File:         AppleTalk.c
  6. ** Written by:   Pete Helme and Jim Luther
  7. ** Modified by:  Eric Soldan
  8. **
  9. ** Copyright © 1989-1991 Apple Computer, Inc.
  10. ** All rights reserved.
  11. */
  12.  
  13.  
  14. /*****************************************************************************/
  15.  
  16.  
  17.  
  18. #include "MacShell.h"            /* Get the MacShell includes/typedefs, etc.    */
  19. #include "MacShellCommon.h"        /* Get the stuff in common with rez.        */
  20. #include "MacShell.protos"        /* Get the prototypes for MacShell.            */
  21.  
  22. #ifndef __ERRORS__
  23. #include <Errors.h>
  24. #endif
  25.  
  26. #ifndef __LISTCONTROL__
  27. #include "ListControl.h"
  28. #endif
  29.  
  30. #ifndef __OSUTILS__
  31. #include <OSUtils.h>
  32. #endif
  33.  
  34. #ifndef __RESOURCES__
  35. #include <Resources.h>
  36. #endif
  37.  
  38.  
  39.  
  40. #define        kATPTimeOutVal            3            /* re-try ATP SendRequest every 3 seconds */
  41. #define        kATPRetryCount            5            /* for five times */
  42. #define        kZonesSize                578            /* size of buffer for zone names */
  43. #define        kGZLCall                0x08000000    /* GetZoneList indicator */
  44. #define        kZIPSocket                6            /* the Zone Information Protocol socket */
  45. #define        kMoreZones                0xFF000000     /* mask to see if more zones to come */
  46. #define        kZoneCount                0x0000FFFF     /* mask to count zones in buffer */
  47.  
  48. #define        kTupleSize    104
  49. #define        kMaxTuples    1
  50.  
  51.  
  52.  
  53. /*****************************************************************************/
  54.  
  55.  
  56.  
  57. #pragma segment myPPC
  58. OSErr    ATInit(void)
  59. {
  60.     return(noErr);
  61. }
  62.  
  63.  
  64.  
  65. /*****************************************************************************/
  66.  
  67.  
  68.  
  69. /* Create the list of zones on the network.  Find a bridge to talk to, if one is
  70. ** present, then ask it for zone names.  Add the names to the passed-in list.
  71. */
  72.  
  73. #pragma segment myPPC
  74. OSErr    DoBuildZoneList(ListHandle listHndl)
  75. {
  76.     ATPParamBlock    atppb;
  77.     char            zones[kZonesSize], *zptr, data[255];
  78.     OSErr            err;
  79.  
  80.     BDSElement    dBDS;                /* the BDS for GetZoneList call */
  81.     short        index, count, i;
  82.     short        ignore;
  83.     short        nodeNetAddress, bridgeNode;
  84.  
  85.     dBDS.buffSize = kZonesSize;                                    /* set up BDS */
  86.     dBDS.buffPtr = zones;
  87.  
  88.     atppb.ATPatpFlags = 0;
  89.  
  90.     /* Get network address of node & node ID of bridge (if any). */
  91.  
  92.     err = GetNodeAddress(&ignore, &nodeNetAddress);
  93.     if (err) return(err);
  94.  
  95.     if (!(bridgeNode = GetBridgeAddress())) return(noErr);
  96.         /* We have added all zero zones to the ist, so we are done. */
  97.  
  98.     atppb.ATPaddrBlock.aNet = nodeNetAddress;
  99.     atppb.ATPaddrBlock.aNode = bridgeNode;            /* Get node of bridge. */
  100.     atppb.ATPaddrBlock.aSocket = kZIPSocket;        /* The socket we want. */
  101.     atppb.ATPreqLength = 0;
  102.     atppb.ATPreqPointer = nil;
  103.     atppb.ATPbdsPointer = (Ptr) &dBDS;
  104.     atppb.ATPnumOfBuffs = 1;
  105.     atppb.ATPtimeOutVal = kATPTimeOutVal;
  106.     atppb.ATPretryCount = kATPRetryCount;
  107.  
  108.     index = 1;
  109.     count = 0;
  110.  
  111.     do {
  112.         atppb.ATPuserData = kGZLCall + index;            /* Indicate GetZoneList request. */
  113.         err = PSendRequest(&atppb, false);                /* Send sync request. */
  114.         if (err) return(err);
  115.  
  116.         count += dBDS.userBytes & kZoneCount;            /* find out how many returned */
  117.         zptr = zones;                                    /* put current pointer at start */
  118.         do {                                            /* get each zone */
  119.             for (i = zptr[0]; i; --i)
  120.                 data[i - 1] = zptr[i];
  121.             CLInsert(listHndl, data, zptr[0], -1, 0);
  122.             zptr += (*zptr + 1 );                        /* bump up current pointer*/
  123.             ++index;                                    /* increment which zone */
  124.         } while(index <= count);
  125.  
  126.     } while ((dBDS.userBytes & kMoreZones) == 0);        /*     keep going until none left */
  127.  
  128.     return(noErr);
  129. }
  130.  
  131.  
  132.  
  133. /*****************************************************************************/
  134.  
  135.  
  136.  
  137. /* Select our zone in the zone list. */
  138.  
  139. #pragma segment myPPC
  140. OSErr    DoSelectMyZone(ListHandle listHndl)
  141. {
  142.     XPPParamBlock    xcall;
  143.     Str32            zone;
  144.     short            zoneLen, i;
  145.     Point            cell;
  146.     OSErr            err;
  147.  
  148.     xcall.XCALL.xppTimeout = 3;
  149.     xcall.XCALL.xppRetry   = 4;
  150.     xcall.XCALL.zipBuffPtr = (Ptr)zone;
  151.     xcall.XCALL.zipInfoField[0] = 0;
  152.     xcall.XCALL.zipInfoField[1] = 0;
  153.  
  154.     if (!(err = GetMyZone(&xcall, false))) {
  155.         for (zoneLen = zone[i = 0]; i < zoneLen; ++i) zone[i] = zone[i + 1];
  156.         cell.h = cell.v = 0;
  157.         if (LSearch(zone, zoneLen, nil, &cell, listHndl)) {
  158.             LSetSelect(true, cell, listHndl);
  159.             LAutoScroll(listHndl);
  160.         }
  161.     }
  162.     return(err);
  163. }
  164.  
  165.  
  166.  
  167. /*****************************************************************************/
  168.  
  169.  
  170.  
  171. /* This routine finds the socket used by the PPC Toolbox (it's the one with
  172. ** type 'PPCToolBox') and gives it a NBP alias (it registers a new NBP name
  173. ** on that socket) with the type passed in newNBPType.  The NameTableEntry
  174. ** record passed to this routine must be allocated globally (or must be a
  175. ** locked block on the heap until RemoveNBPAlias is called).  The variable
  176. ** newEntity is filled in with the new entity name and is returned to the
  177. ** function's caller so it can be passed to the RemoveNBPAlias function
  178. ** (below).  You'll get an error if any call fails, if an entity of type
  179. ** 'PPCToolBox' is not found (usually because either Program Linking isn't
  180. ** enabled or AppleTalk is disabled), or if RegisterName fails because >100
  181. ** copies of your application are running on the one machine.
  182. */
  183.  
  184. #pragma segment myPPC
  185. OSErr    AddPPCNBPAlias(NamesTableEntry *theNTE, Str32 newNBPType, EntityName *newEntity)
  186. {
  187.     OSErr            err;
  188.     MPPParamBlock    pb;
  189.     char            keepSelfFlag;
  190.     short            keepResFile, origLen, num, len;
  191.  
  192.     EntityName        myEntityName;
  193.  
  194.     AddrBlock        myAddrBlock;
  195.     char            myRetBuff[kTupleSize];
  196.     Handle            machineNameHndl;
  197.     Str32            machineName;
  198.  
  199.     pb.SETSELF.newSelfFlag = 1;
  200.     err = PSetSelfSend(&pb, false);                        /* Turn on self-send. */
  201.     if (err) return(err);
  202.  
  203.     keepSelfFlag = pb.SETSELF.oldSelfFlag;                /* Keep old self-send flag. */
  204.  
  205.     keepResFile = CurResFile();
  206.     UseResFile(0);
  207.     machineNameHndl = GetResource('STR ', -16413);        /* Get machine name. */
  208.     UseResFile(keepResFile);
  209.  
  210.     if (!machineNameHndl) {
  211.         pb.SETSELF.newSelfFlag = keepSelfFlag;
  212.         PSetSelfSend(&pb, false);
  213.         return(ResError());
  214.     }
  215.  
  216.     pstrcpy((char *)machineName, (char *)*machineNameHndl);        /* Keep a copy of the machine name.   */
  217.     ReleaseResource(machineNameHndl);            /* Release the machine name resource. */
  218.  
  219.     NBPSetEntity((Ptr)&myEntityName, (Ptr)machineName, (Ptr)"\pPPCToolBox", (Ptr)"\p*");
  220.  
  221.     pb.NBPinterval    = 1;                    /* We want to build the entity name using */
  222.     pb.NBPcount       = 1;                    /* the machine name and 'PPCToolBox'.     */
  223.     pb.NBPentityPtr   = (Ptr)&myEntityName;
  224.     pb.NBPretBuffPtr  = myRetBuff;
  225.     pb.NBPretBuffSize = (kTupleSize * kMaxTuples);
  226.     pb.NBPmaxToGet    = kMaxTuples;
  227.  
  228.     if (!(err = PLookupName(&pb, false))) {        /* If lookup was okay... */
  229.         if (pb.NBPnumGotten) {                    /* If entity was found... */
  230.                                     /* ...we found the socket used by the PPC Toolbox. */
  231.                                     /* This means that there is a socket, due to program */
  232.                                     /* linking being turned on. */
  233.  
  234.             NBPExtract(myRetBuff, pb.NBPnumGotten, 1, &myEntityName, &myAddrBlock);
  235.                 /* Break the tuple into component parts. */
  236.  
  237.             for (origLen = machineName[num = 0]; num < 100;) {
  238.  
  239.                 pb.NBPinterval   = 7;
  240.                 pb.NBPcount      = 5;
  241.                 pb.NBPentityPtr  = (Ptr)theNTE;
  242.                 pb.NBPverifyFlag = 1;
  243.  
  244.                 NBPSetNTE((Ptr)theNTE, (Ptr)machineName, (Ptr)newNBPType, (Ptr)"\p*", myAddrBlock.aSocket);
  245.                 NBPSetEntity((Ptr)newEntity, (Ptr)machineName,(Ptr) newNBPType, (Ptr)"\p*");
  246.                 err = PRegisterName(&pb, false);
  247.  
  248.                 if (err != nbpDuplicate) break;
  249.                     /* We registered the name, (or badness happened), so we are done. */
  250.  
  251.                 len = 31;        /* The name we tried already exists, so make up an alternate. */
  252.                 if (++num > 9) --len;
  253.                 if (origLen > len) origLen = len;
  254.                 machineName[0] = origLen;
  255.                 appendi2pstr((char *)machineName, num);
  256.             }
  257.         }
  258.     }
  259.  
  260.     pb.SETSELF.newSelfFlag = keepSelfFlag;
  261.     PSetSelfSend(&pb, false);
  262.  
  263.     return(err);
  264. }
  265.  
  266.  
  267.  
  268.  
  269. /*****************************************************************************/
  270.  
  271.  
  272.  
  273. /* This function removes the entity specified by theEntity from the registered
  274. ** names queue.  You'll get an error if theEntity hasn't been registered on
  275. ** this Macintosh with RegisterName.
  276. */
  277.  
  278.  
  279. #pragma segment myPPC
  280. OSErr    RemoveNBPAlias(EntityPtr theEntity)
  281. {
  282.     MPPParamBlock    pb;
  283.  
  284.     pb.MPPioCompletion = nil;
  285.     pb.NBPentityPtr    = (Ptr)theEntity;
  286.     return(PRemoveName(&pb, false));
  287. }
  288.  
  289.  
  290.  
  291.